package com.cencat.driver.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cencat.driver.entity.Route;
import com.cencat.driver.service.RouteService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 路由管理控制器
 * 提供路由相关的RESTful API接口
 * 
 * @author cencat
 * @since 2024-01-01
 */
@RestController
@RequestMapping("/api/route")
@Tag(name = "路由管理", description = "路由管理相关接口")
@CrossOrigin(origins = "*")
public class RouteController {

    @Autowired
    private RouteService routeService;

    /**
     * 分页查询路由列表
     */
    @GetMapping("/page")
    @Operation(summary = "分页查询路由列表", description = "根据条件分页查询路由信息")
    public IPage<Route> getRoutePage(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer current,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") Integer size,
            @Parameter(description = "起点") @RequestParam(required = false) String startPoint,
            @Parameter(description = "终点") @RequestParam(required = false) String endPoint,
            @Parameter(description = "路由状态") @RequestParam(required = false) Integer status) {
        return routeService.getRoutePage(current, size, startPoint, endPoint, status);
    }

    /**
     * 根据ID查询路由详情
     */
    @GetMapping("/{id}")
    @Operation(summary = "查询路由详情", description = "根据路由ID查询详细信息")
    public Route getRouteById(@Parameter(description = "路由ID") @PathVariable Long id) {
        return routeService.getById(id);
    }

    /**
     * 根据起点和终点查询路由
     */
    @GetMapping("/search")
    @Operation(summary = "搜索路由", description = "根据起点和终点搜索路由")
    public List<Route> getRoutesByPoints(
            @Parameter(description = "起点") @RequestParam String startPoint,
            @Parameter(description = "终点") @RequestParam String endPoint) {
        return routeService.getRoutesByPoints(startPoint, endPoint);
    }

    /**
     * 创建新路由
     */
    @PostMapping
    @Operation(summary = "创建路由", description = "创建新的路由信息")
    public boolean createRoute(@RequestBody Route route) {
        return routeService.createRoute(route);
    }

    /**
     * 更新路由信息
     */
    @PutMapping("/{id}")
    @Operation(summary = "更新路由", description = "更新路由信息")
    public boolean updateRoute(
            @Parameter(description = "路由ID") @PathVariable Long id,
            @RequestBody Route route) {
        route.setId(id);
        return routeService.updateById(route);
    }

    /**
     * 更新路由状态
     */
    @PutMapping("/{id}/status")
    @Operation(summary = "更新路由状态", description = "更新路由的状态")
    public boolean updateRouteStatus(
            @Parameter(description = "路由ID") @PathVariable Long id,
            @Parameter(description = "状态") @RequestParam Integer status) {
        return routeService.updateRouteStatus(id, status);
    }

    /**
     * 分配司机到路由
     */
    @PutMapping("/{id}/assign-driver")
    @Operation(summary = "分配司机", description = "为路由分配司机")
    public boolean assignDriver(
            @Parameter(description = "路由ID") @PathVariable Long id,
            @Parameter(description = "司机ID") @RequestParam Long driverId) {
        return routeService.assignDriver(id, driverId);
    }

    /**
     * 计算路由距离
     */
    @PostMapping("/calculate-distance")
    @Operation(summary = "计算路由距离", description = "计算两点之间的路由距离")
    public Double calculateRouteDistance(
            @Parameter(description = "起点") @RequestParam String startPoint,
            @Parameter(description = "终点") @RequestParam String endPoint) {
        return routeService.calculateRouteDistance(startPoint, endPoint);
    }

    /**
     * 获取路由统计信息
     */
    @GetMapping("/statistics")
    @Operation(summary = "路由统计", description = "获取路由统计信息")
    public Map<String, Object> getRouteStatistics() {
        return routeService.getRouteStatistics();
    }

    /**
     * 根据司机ID查询路由
     */
    @GetMapping("/driver/{driverId}")
    @Operation(summary = "查询司机路由", description = "根据司机ID查询相关路由")
    public List<Route> getRoutesByDriverId(@Parameter(description = "司机ID") @PathVariable Long driverId) {
        return routeService.getRoutesByDriverId(driverId);
    }

    /**
     * 获取活跃路由列表
     */
    @GetMapping("/active")
    @Operation(summary = "活跃路由列表", description = "获取所有活跃状态的路由")
    public List<Route> getActiveRoutes() {
        return routeService.getActiveRoutes();
    }
}